package com.wc.blue_bridge_history.Java15B.B_类斐波那契循环数;

/**
 * @Author congge
 * @Date 2024/4/13 11:55
 * @description
 */
public class Main {
    public static void main(String[] args) {
        int n = (int) 1e7;
        long[] f = new long[2000];
        for (int i = n; i >= 0; i--) {
            int len = 0;
            int m = i;
            long maxV = 0;
            while (m > 0) {
                f[++len] = m % 10;
                m /= 10;
                maxV = Math.max(maxV, f[len]);
            }
            for (int l = 1, r = len; l < r; l++, r--) {
                long tmp = f[l];
                f[l] = f[r];
                f[r] = tmp;
            }

            boolean flg = false;
            while (true) {
                long sum = 0;
                for (int j = 1; j < len; j++) {
                    sum += f[j];
                }
                boolean flg2 = false;
                for (int j = len + 1; ; j++) {
                    sum += f[j - 1];
                    f[j] = sum;
                    if (f[j] == i) {
                        flg = true;
                        break;
                    }
                    if (f[j] <= maxV || f[j] > i) {
                        flg2 = true;
                        break;
                    }
                    maxV = f[j];
                    sum -= f[j - len];
                }
                if (flg || flg2) break;
            }
            if (flg) {
                // 7913837
                System.out.println(i);
                break;
            }
        }
    }
}
